var vm = new Vue({
    el:'#wrapper',
    data:{
        loginUserInfo:{},//登录用户信息
        menuList:[],//系统菜单
        menuHtml:""//系统菜单HTML
    },
    methods:{
        //退出系统
        logout:function() {
            parent.layer.confirm("你确定要退出系统吗！", {btn: ['确定','取消']},
                function(){//确定事件
                    $.ajax({
                        url: g_comm.manager_url + "/sys/logout",
                        success: function (result) {
                            if(result.code == 0){
                                clearToken();//清空本地token信息
                                parent.location.href ='views/login.html';
                            } else {
                                parent.layer.alert("退出系统失败", function(index){
                                    parent.layer.close(index);
                                });
                            }
                        }
                    });
                });
        },
        //获取登录用户信息
        getLoginUserInfo:function() {
            //使用jquery发起ajax请求
            var _this = this; //临时存储vue对象实例，下面的ajax调用要用到
            $.ajax({
                url: g_comm.manager_url + "/sys/getLoginUserInfo",
                success: function (result) {
                    if(result.code == 0){
                        _this.loginUserInfo = result.data;
                        if(_this.loginUserInfo.headImg==""){
                            //默认头像
                            _this.loginUserInfo.headImg = "img/sys/defaultHeadImg.png";
                        }
                        //登录用户信息
                        localStorage.setItem("loginUserInfo",JSON.stringify(_this.loginUserInfo));
                        //登录用户权限
                        localStorage.setItem("speed-test-perms",_this.loginUserInfo.perms);
                    }
                }
            });
        },
        //获取导航菜单
        getNavMenu:function () {
            //使用jquery发起ajax请求
            var _this = this; //临时存储vue对象实例，下面的ajax调用要用到
            $.ajax({
                url: g_comm.manager_url + "/sysResource/navMenu",
                success: function (result) {
                    if(result.code == 0){
                        _this.menuList = result.data;
                        //构建菜单
                        _this.menuHtml = _this.createMenu( _this.menuList,"");
                        //将菜单的html添加到side-menu
                        $("#side-menu").append( _this.menuHtml);
                        //重新渲染菜单
                        $("#side-menu").metisMenu();
                    }
                }
            });
        },
        /**
         * 生成菜单
         */
        createMenu:function(menuList, menuHtml){
            for (var i=0;i<menuList.length;i++) {
                var menu = menuList[i];
                //如果有子节点，则遍历该子节点
                if (menu.childMenus!=undefined && menu.childMenus.length > 0) {
                    var subMenuItemHtml="";
                    //遍历子菜单
                    for(var j=0;j<menu.childMenus.length;j++){
                        var childMenu = menu.childMenus[j];
                        //如果有子节点，则遍历该子节点
                        if(childMenu.childMenus!=undefined && childMenu.childMenus.length > 0){
                            //递归遍历子菜单
                            var subSubMenuItemHtml = this.createMenu(childMenu.childMenus,"");
                            var subMenuItem=String.format('<li><a href="{0}"><i class="fa fa-{1}"></i><span class="nav-label">{2}</span><span class="fa arrow"></span></a><ul class="nav nav-level">{3}</ul></li>'
                                ,childMenu.href
                                ,childMenu.icon
                                ,childMenu.menuName
                                ,subSubMenuItemHtml);
                            subMenuItemHtml+=subMenuItem;
                        }else{
                            var subMenuItem=String.format('<li><a class="J_menuItem" href="{0}" data-index="{3}"><i class="fa fa-{1}"></i><span class="nav-label">{2}</span></a></li>'
                                ,childMenu.href
                                ,childMenu.icon
                                ,childMenu.menuName
                                ,j+1);
                            subMenuItemHtml+=subMenuItem;
                        }
                    }

                    var menuItemHtml=String.format('<li><a href="{0}"><i class="fa fa-{1}"></i><span class="nav-label">{2}</span><span class="fa arrow"></span></a><ul class="nav nav-level">{3}</ul></li>'
                        ,menu.href
                        ,menu.icon
                        ,menu.menuName
                        ,subMenuItemHtml);
                    menuHtml+=menuItemHtml;
                }
                else {
                    var menuUrl = "";
                    if(menu.href!=null && menu.href !="" && menu.href.indexOf("data_center.html")==-1){
                        menuUrl = menu.href
                    }
                    //组装没有子菜单的菜单项的Html
                    var menuItemHtml =String.format('<li><a class="J_menuItem" href="{0}"><i class="fa fa-{1}"></i><span class="nav-label">{2}</span></a></li>',menuUrl,menu.icon,menu.menuName);
                    menuHtml+=menuItemHtml;
                }
            }
            return menuHtml;
        }
    },
    //在模板渲染成html后调用，通常是初始化页面完成后
    mounted:function () {
        this.getLoginUserInfo();
        this.getNavMenu();//获取导航菜单
    }
});